﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.TKKeToan 
{
    public partial class frmXuLy : Form
    {
       
        #region Delegate
        public delegate void GetString(Boolean capnhat);
        public GetString GetData;
        private Boolean kiemtrasua = false;
        public static bool CheckClose = false;
        #endregion

        #region Khoi Tao
        private Entities.GetCombobox[] combox;
        private int MaTKKT = 0;
        private string ID = string.Empty;

        public frmXuLy()
        {
            InitializeComponent();
            Event();
        }
        private Entities.TKKeToan taikhoanketoan;
        public Entities.TKKeToan TaiKhoanKeToan
        {
            get { return taikhoanketoan; }
            set { taikhoanketoan = value; }
        }
        private string hanhdong;
        public string Hanhdong
        {
            get { return hanhdong; }
            set { hanhdong = value; }
        }
        public frmXuLy(string hanhdong, Entities.TKKeToan taikhoanketoan)
        {
            InitializeComponent();
            Event();
            this.hanhdong = hanhdong;
            this.taikhoanketoan = taikhoanketoan;
        }
        #endregion

        #region Event
        private void Event()
        {
            btnThoat.MouseDown += new MouseEventHandler(imgThoat_MouseDown);
            cbbNhomTKKT.SelectedValueChanged += new EventHandler(cbbNhomTKKT_SelectionChanged);
            this.Closing += new System.ComponentModel.CancelEventHandler(frmXuLyTKKT_Closing);
        }

        private void cbbNhomTKKT_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                ID = cbbNhomTKKT.SelectedValue.ToString();
            }
            catch 
            {}
        }

        private void imgThem_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (this.hanhdong == Common.Constants.GiaTriCacForm.Insert)
                {
                    this.Them();
                }
                if (this.hanhdong == Common.Constants.GiaTriCacForm.Update)
                {
                    this.Sua();
                }
            }
            catch
            {  }
        }

        private void imgThoat_MouseDown(object sender, MouseEventArgs e)
        {
            this.Thoat();
        }

        private void frmXuLy_Load(object sender, EventArgs e)
        {
            try
            {
                this.WindowState = FormWindowState.Normal;
                this.HienThiNhomTKKT();
                if (this.hanhdong == Common.Constants.GiaTriCacForm.Insert)
                {
                }
                if (this.hanhdong == Common.Constants.GiaTriCacForm.Update)
                {
                    this.XuLyDuLieuSua();
                    txtMaTKKT.Enabled = false;
                }
                txtMaTKKT.Focus();
                lblTenNhanVien.Text = UtilitiesVariable.User.TenDangNhap;
            }
            catch 
            {  }
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Escape)
                {
                    this.Thoat();
                }
                if (e.KeyCode == Keys.F2)
                {
                    if (this.hanhdong == Common.Constants.GiaTriCacForm.ThemTKKeToan)
                    {
                        this.Them();
                    }
                    if (this.hanhdong == Common.Constants.GiaTriCacForm.SuaTKKeToan)
                    {
                        this.Sua();
                    }
                }
            }
            catch 
            {  }
        }
        #endregion

        #region Do du lieu sua
        private void XuLyDuLieuSua()
        {
            try
            {
                MaTKKT = this.taikhoanketoan.TKKeToanID;
                txtMaTKKT.Text = this.taikhoanketoan.MaTKKeToan;
                txtTenTKKT.Text = this.taikhoanketoan.TenTaiKhoan;
                cbbNhomTKKT.Text = this.taikhoanketoan.MaNhomTKKeToan;
                HienThiCombox(this.taikhoanketoan.MaNhomTKKeToan);
                txtGhiChu.Text = this.taikhoanketoan.GhiChu;
            }
            catch 
            {  }
        }
        #endregion

        #region Hien Thi
        private Boolean getData(string ma)
        {
            Boolean kt = true;
            try
            {
                Entities.TKKeToan nhom = new Entities.TKKeToan("0");
                Entities.TKKeToan[] ntk = Common.StaticVariable.InterfaceObject.SelectTKKeToan(nhom);

                for (int k = 0; k < ntk.Length; k++)
                {
                    if (ntk[k].MaTKKeToan == ma)
                    { kt = false; break; }
                    else
                    { continue; }
                }
            }
            catch 
            {  kt = false; }
            return kt;
        }

        private void HienThiCombox(string ma)
        {
            try
            {
                for (int k = 0; k < cbbNhomTKKT.Items.Count; k++)
                {
                    Entities.GetCombobox id = (Entities.GetCombobox)cbbNhomTKKT.Items[k];
                    if (id.TenCotKhoaChinh == ma)
                    {
                        cbbNhomTKKT.SelectedIndex = k;
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch 
            {  }
        }
        private void HienThiNhomTKKT()
        {
            try
            {
                Entities.GetCombobox gitri = new Entities.GetCombobox(Common.Constants.GiaTriCacForm.NhomTKKeToan, Common.Constants.GiaTriCacForm.MaNhomTKKeToan, Common.Constants.GiaTriCacForm.TenNhomTKKeToan);
                combox = Common.StaticVariable.InterfaceObject.SelectComboxCacBang(gitri);

                Common.Utilities.XuLyComboBox(cbbNhomTKKT,combox, Common.Constants.GiaTriCacForm.CotTenHienThi, Common.Constants.GiaTriCacForm.CotKhoaChinh);
            }
            catch 
            {  }
        }

        #endregion

        #region CheckPerMission
        private Boolean CheckPerMission()
        {
            return true;
        }
        #endregion

        #region Validate
        private Boolean Validate(Entities.TKKeToan xuly)
        {
            Boolean kt = true;
            try
            {
                try
                {
                    if (txtMaTKKT.Text != string.Empty)
                    {
                        int ma = int.Parse(txtMaTKKT.Text);
                    }
                }
                catch 
                {
                    
                    if (txtMaTKKT.Text != string.Empty)
                    {
                        MessageBox.Show("Định dạng nhập không đúng", Common.Constants.GiaTriCacForm.HeThongCanhBao);
                        txtMaTKKT.Text = "";
                        txtMaTKKT.Focus();
                        kt = false;
                        return kt;
                    }
                }
                if (xuly.MaTKKeToan == string.Empty)
                {
                    kt = false;
                    txtMaTKKT.Focus();
                    MessageBox.Show(Common.Constants.GiaTriCacForm.NhapMaTKKT, Common.Constants.GiaTriCacForm.HeThongCanhBao);
                    return kt;
                }
                if (xuly.TenTaiKhoan == string.Empty)
                {
                    kt = false;
                    txtTenTKKT.Focus();
                    MessageBox.Show(Common.Constants.GiaTriCacForm.NhapTenTKKT, Common.Constants.GiaTriCacForm.HeThongCanhBao);
                    return kt;
                }
                if (xuly.MaNhomTKKeToan == string.Empty)
                {
                    kt = false;
                    cbbNhomTKKT.Focus();
                    MessageBox.Show(Common.Constants.GiaTriCacForm.NhapMaNhomTKKT, Common.Constants.GiaTriCacForm.HeThongCanhBao);
                    return kt;
                }
            }
            catch 
            {  kt = false; }
            return kt;
        }
        #endregion

        #region CheckConflick
        private Entities.TKKeToan SoSanh()
        {
            Entities.TKKeToan sosanh = null;
            try
            {
                Entities.TKKeToan gitri = new Entities.TKKeToan(this.taikhoanketoan.MaTKKeToan);
                Entities.TKKeToan[] nhom = Common.StaticVariable.InterfaceObject.SelectTKKeToan(gitri);

                sosanh = nhom[0];
            }
            catch 
            {  sosanh = null; }
            return sosanh;
        }

        private Boolean CheckConflick(Entities.TKKeToan manggoc, Entities.TKKeToan sosanh)
        {
            Boolean kt = true;
            try
            {
                this.taikhoanketoan = sosanh;
                if (manggoc.MaTKKeToan != sosanh.MaTKKeToan)
                {
                    kt = false;
                    txtMaTKKT.Text = sosanh.MaTKKeToan;
                    MessageBox.Show(Common.Constants.GiaTriCacForm.NhapMaTKKTTD, Common.Constants.GiaTriCacForm.HeThongCanhBao);
                    return kt;
                }
                if (manggoc.TenTaiKhoan != sosanh.TenTaiKhoan)
                {
                    kt = false;
                    cbbNhomTKKT.Text = sosanh.TenTaiKhoan;
                    MessageBox.Show(Common.Constants.GiaTriCacForm.NhapTenTKKTTD, Common.Constants.GiaTriCacForm.HeThongCanhBao);
                    return kt;
                }
                if (manggoc.MaNhomTKKeToan != sosanh.MaNhomTKKeToan)
                {
                    kt = false;
                    HienThiCombox(sosanh.MaNhomTKKeToan);
                    MessageBox.Show(Common.Constants.GiaTriCacForm.NhapNhomTKKTTD, Common.Constants.GiaTriCacForm.HeThongCanhBao);
                    return kt;
                }
                if (manggoc.GhiChu != sosanh.GhiChu)
                {
                    kt = false;
                    txtGhiChu.Text = sosanh.GhiChu;
                    MessageBox.Show(Common.Constants.GiaTriCacForm.GhiChuThayDoi, Common.Constants.GiaTriCacForm.GhiChuThayDoi);
                    return kt;
                }
                if (manggoc.Deleted != sosanh.Deleted)
                {
                    kt = false;
                    MessageBox.Show(Common.Constants.GiaTriCacForm.DeleteThayDoi, Common.Constants.GiaTriCacForm.DeleteThayDoi);
                    return kt;
                }
            }
            catch 
            {  kt = false; }
            return kt;
        }
        #endregion

        #region Xu Ly
        private Entities.TKKeToan XuLyDuLieu(string hanhdong)
        {
            Entities.TKKeToan xuly = null;
            try
            {
                xuly = new Entities.TKKeToan();
                xuly.HanhDong = hanhdong;
                xuly.TKKeToanID = this.MaTKKT;
                xuly.MaTKKeToan = txtMaTKKT.Text;
                xuly.TenTaiKhoan = txtTenTKKT.Text;
                try
                {
                    xuly.MaNhomTKKeToan = this.ID;
                }
                catch { }
                xuly.GhiChu = txtGhiChu.Text;
                xuly.Deleted = false;
                xuly.TaiKhoan = UtilitiesVariable.User;
            }
            catch 
            {  xuly = null; }
            return xuly;
        }

        private void InsertUpdate(string hanhdong)
        {
            try
            {
                Entities.TKKeToan nh = XuLyDuLieu(hanhdong);
                if (Validate(nh) != true)
                {
                    return;
                }
                int i = Common.StaticVariable.InterfaceObject.InsertUpdateTKKeToan(nh);

                if (i != 0)
                {
                     
                    if (hanhdong == Common.Constants.GiaTriCacForm.Insert)
                    {
                        CheckClose = true;
                        this.Close();
                    }
                    else
                    {
                        CheckClose = false;
                        kiemtrasua = true;
                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show(Common.Constants.GiaTriCacForm.ThatBai, Common.Constants.GiaTriCacForm.HeThongCanhBao);
                }
            }
            catch 
            {  }
        }

        private void Them()
        {
            try
            {
                if (CheckPerMission() != true)
                {
                    return;
                }
                if (this.getData(txtMaTKKT.Text) == false)
                {
                    MessageBox.Show("Mã không đúng", Common.Constants.GiaTriCacForm.HeThongCanhBao);
                    txtMaTKKT.Text = "";
                    txtMaTKKT.Focus();
                    return;
                }
                InsertUpdate(Common.Constants.GiaTriCacForm.Insert);
               
            }
            catch 
            {  }
        }

        private void Sua()
        {
            try
            {
                if (CheckPerMission() != true)
                {
                    return;
                }
                if (CheckConflick(this.taikhoanketoan, SoSanh()) != true)
                {
                    return;
                }
                InsertUpdate(Common.Constants.GiaTriCacForm.Update);
            }
            catch 
            {  }
        }
        #endregion

        #region Thoat
        private void frmXuLyTKKT_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                if (CheckClose == false && kiemtrasua == false)
                {
                    System.Windows.Forms.DialogResult giatri = System.Windows.Forms.MessageBox.Show(Common.Constants.GiaTriCacForm.ThongBaoThoat, Common.Constants.GiaTriCacForm.HeThongCanhBao, System.Windows.Forms.MessageBoxButtons.YesNo);
                    {
                        if (giatri == System.Windows.Forms.DialogResult.Yes)
                        {
                             
                            e.Cancel = false;
                        }
                        else
                        {
                            e.Cancel = true;
                        }
                    }
                }
                GetData(true);
            }
            catch 
            {  }
        }
        private void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult giatri = System.Windows.Forms.MessageBox.Show(Common.Constants.GiaTriCacForm.ThongBaoThoat, Common.Constants.GiaTriCacForm.HeThongCanhBao, System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (giatri == System.Windows.Forms.DialogResult.Yes)
                    {
                         
                        kiemtrasua = true;
                        CheckClose = false;
                        this.Close();
                    }
                }
            }
            catch 
            {  }
        }
        #endregion
    }
}
